package com.smartcruise.report;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.base.BaseActivity;
import com.smartcruise.data.ReportData;
import com.smartcruise.data.cruiseData.CruiseContract;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.util.IntentUtils;
import com.smartcruise.util.Key;
import com.smartcruise.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * Created by FengChaoQun
 * on 2018/1/23 0023
 * 通报数据编辑界面
 */

public class ReportDataEditActivity extends BaseActivity implements ReportDataContract.View {

    @BindView(R.id.leftArrow)
    ImageView leftArrow;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rightText)
    TextView rightText;
    @BindView(R.id.eventType)
    TextView eventType;
    @BindView(R.id.camera)
    ImageView camera;
    @BindView(R.id.audio)
    ImageView audio;
    @BindView(R.id.video)
    ImageView video;
    @BindView(R.id.scan)
    ImageView scan;
    @BindView(R.id.audioGrid)
    GridView audioGrid;
    @BindView(R.id.imageGrid)
    GridView imageGrid;
    @BindView(R.id.videoList)
    ListView videoList;
    @BindView(R.id.delete)
    Button delete;
    @BindView(R.id.templeSave)
    Button templeSave;
    @BindView(R.id.upReport)
    Button upReport;
    @BindView(R.id.content)
    EditText content;
    @BindView(R.id.statusText)
    TextView statusText;
    @BindView(R.id.evaluation)
    TextView evaluation;
    @BindView(R.id.statusLay)
    LinearLayout statusLay;
    @BindView(R.id.auditPeople)
    TextView auditPeople;

    private ReportDataContract.Presenter mPresenter;
    private EditType editType;
    private ReportData reportData;
    private CruiseData cruiseData;

    /**
     * description:创建一个新的通报
     *
     * @param cruiseId 对应的巡航任务id 如果为空 就是普通的通报
     */

    public static void start(Context context, String cruiseId) {
        Intent intent = new Intent(context, ReportDataEditActivity.class);
        intent.putExtra(IntentUtils.TYPE, EditType.CREATE);
        intent.putExtra(IntentUtils.DATA, cruiseId);
        context.startActivity(intent);
    }

    /**
     * description:打开一个通报
     *
     * @param editType 编辑状态
     * @param id       通报的id
     */

    public static void start(Context context, EditType editType, String id) {
        Intent intent = new Intent(context, ReportDataEditActivity.class);
        intent.putExtra(IntentUtils.TYPE, editType);
        if (id != null) {
            intent.putExtra(IntentUtils.ID, id);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_data_edit);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        editType = (EditType) getIntent().getSerializableExtra(IntentUtils.TYPE);
        String reportId = getIntent().getStringExtra(IntentUtils.ID);
        String cruiseId = getIntent().getStringExtra(IntentUtils.DATA);
        if (editType == null) {
            showToast(getString(R.string.jumpException));
            finish();
            return;
        }

        if (editType == EditType.MODIFY || editType == EditType.VIEW) {//编辑或浏览一个通报的时候 先要找到该通报数据
            if (TextUtils.isEmpty(reportId)) {
                showToast("没有通报id");
                finish();
                return;
            }
            reportData = realm.where(ReportData.class)
                    .equalTo(Key.id, reportId)
                    .findFirst();
            if (reportData == null) {
                showToast(getString(R.string.nothingToShow));
                finish();
                return;
            }

            if (reportData.isAuditSuccess()) {
                statusLay.setVisibility(View.VISIBLE);
                statusLay.setBackgroundColor(getResources().getColor(R.color.titleColorAlpha0_5));
                statusText.setText("已处理");
                evaluation.setText("评价:" + reportData.getEvaluation());
            } else if (CruiseContract.AUDIT_FAIL.equals(reportData.getAuditResult())) {
                statusLay.setVisibility(View.VISIBLE);
                statusLay.setBackgroundColor(getResources().getColor(R.color.sosOrange));
                statusText.setText("被驳回");
                evaluation.setText(reportData.getEvaluation());
            }

        }

        if (editType == EditType.VIEW) {//预览模式不能编辑
            content.setFocusable(false);
            content.setFocusableInTouchMode(false);
            camera.setVisibility(View.GONE);
            video.setVisibility(View.GONE);
            audio.setVisibility(View.GONE);
            scan.setVisibility(View.GONE);
        }

        if (editType == EditType.CREATE && !TextUtils.isEmpty(cruiseId)) {//创建一个巡航任务的通报 先要找到对应的巡航任务
            cruiseData = realm.where(CruiseData.class)
                    .equalTo(Key.id, cruiseId)
                    .findFirst();
            if (cruiseData == null) {
                showToast("没有找到对应的巡航任务");
                finish();
                return;
            }
        }

        mPresenter = new ReportDataPresenterImp(this, reportData, cruiseData, editType);
        mPresenter.start();
    }

    @Override
    public void setData(BaseAdapter audio, BaseAdapter image, BaseAdapter video) {
        audioGrid.setAdapter(audio);
        notifyAudiosChange(audio);
        imageGrid.setAdapter(image);
        notifyImagesChange(image);
        videoList.setAdapter(video);
        notifyVideosChange(video);
    }

    @Override
    public void setType(String type) {
        eventType.setText(type);
        eventType.setTextColor(getResources().getColor(R.color.blackGray));
    }

    @Override
    public String getType() {
        String type = eventType.getText().toString();
        if (type.equals(getString(R.string.pleaseSelect))) {
            return null;
        }
        return eventType.getText().toString();
    }

    @Override
    public void setAuditPeople(String personName) {
        auditPeople.setText(personName);
    }

    @Override
    public void setText(String text) {
        content.setText(text);
    }

    @Override
    public String getText() {
        return content.getText().toString();
    }

    @Override
    public void notifyAudiosChange(BaseAdapter baseAdapter) {
        if (baseAdapter.isEmpty()) {
            audioGrid.setVisibility(View.GONE);
        } else {
            audioGrid.setVisibility(View.VISIBLE);
            if (baseAdapter.getCount() > 3) {
                ViewGroup.LayoutParams layoutParams = audioGrid.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(200);
                audioGrid.setLayoutParams(layoutParams);
            } else {
                ViewGroup.LayoutParams layoutParams = audioGrid.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(100);
                audioGrid.setLayoutParams(layoutParams);
            }
        }
        baseAdapter.notifyDataSetChanged();
    }

    @Override
    public void notifyImagesChange(BaseAdapter baseAdapter) {
        if (baseAdapter.isEmpty()) {
            imageGrid.setVisibility(View.GONE);
        } else {
            imageGrid.setVisibility(View.VISIBLE);
            if (baseAdapter.getCount() > 3) {
                ViewGroup.LayoutParams layoutParams = imageGrid.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(200);
                imageGrid.setLayoutParams(layoutParams);
            } else {
                ViewGroup.LayoutParams layoutParams = imageGrid.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(100);
                imageGrid.setLayoutParams(layoutParams);
            }
        }
        baseAdapter.notifyDataSetChanged();
    }

    @Override
    public void notifyVideosChange(BaseAdapter baseAdapter) {
        if (baseAdapter.isEmpty()) {
            videoList.setVisibility(View.GONE);
        } else {
            videoList.setVisibility(View.VISIBLE);
            if (baseAdapter.getCount() == 1) {
                ViewGroup.LayoutParams layoutParams = videoList.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(200);
                videoList.setLayoutParams(layoutParams);
            } else if (baseAdapter.getCount() == 2) {
                ViewGroup.LayoutParams layoutParams = videoList.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(400);
                videoList.setLayoutParams(layoutParams);
            } else if (baseAdapter.getCount() == 3) {
                ViewGroup.LayoutParams layoutParams = videoList.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = Utils.px(600);
                videoList.setLayoutParams(layoutParams);
            }
        }
        baseAdapter.notifyDataSetChanged();
    }

    @Override
    public void showDeleteButton(boolean show) {
        delete.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showSaveButton(boolean show) {
        templeSave.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showUploadButton(boolean show) {
        upReport.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    @OnClick(R.id.leftArrow)
    public void onLeftArrowClicked() {
        mPresenter.clickOnBack();
    }

    @OnClick(R.id.rightText)
    public void onRightTextClicked() {
        showToast(getString(R.string.onDeveloping));
    }

    @OnClick(R.id.eventType)
    public void onEventTypeClicked() {
        if (editType == EditType.VIEW) {
            return;
        }
        mPresenter.selectType();
    }

    @OnClick(R.id.camera)
    public void onCameraClicked() {
        mPresenter.openCamera();
    }

    @OnClick(R.id.audio)
    public void onAudioClicked() {
        mPresenter.openAudioRecord();
    }

    @OnClick(R.id.video)
    public void onVideoClicked() {
        mPresenter.openVideo();
    }

    @OnClick(R.id.scan)
    public void onScanClicked() {
        mPresenter.openScan();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mPresenter.onActivityResult(requestCode, resultCode, data);
    }

    @OnClick(R.id.delete)
    public void onDeleteClicked() {
        mPresenter.delete();
    }

    @OnClick(R.id.templeSave)
    public void onTempleSaveClicked() {
        mPresenter.clickOnSave();
    }

    @OnClick(R.id.upReport)
    public void onUpReportClicked() {
        mPresenter.clickOnUpload();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (JCVideoPlayer.backPress()) {
                return true;
            }
            mPresenter.clickOnBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick(R.id.auditPeople)
    public void onViewClicked() {
        mPresenter.selectAuditPeople();
    }
}
